﻿using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace KZ.Express.DAL
{
    public class PriceListDetailDAL : DataAccessBase
    {
        /// <summary>
        /// Select price both original and customer
        /// </summary>
        /// <param name="priceListID">id of price list</param>
        /// <param name="carrierID">id of carrier to get zone</param>
        /// <param name="countryID">id of country to get zone</param>
        /// <param name="weight">weight to get price</param>
        /// <returns></returns>
        public decimal SelectPrice(int priceListID, int carrierID, int countryID, string type, decimal weight)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("PriceListID", priceListID));
            parameters.Add(new SqlParameter("CarrierID", carrierID));
            parameters.Add(new SqlParameter("CountryID", countryID));
            parameters.Add(new SqlParameter("Type", type));
            parameters.Add(new SqlParameter("Weight", weight));
            parameters.Add(new SqlParameter("ListPrice", SqlDbType.Decimal, 100));
            parameters.Last().Scale = 2;
            parameters.Last().Direction = ParameterDirection.Output;

            _helper.ExecuteNonQuery("proc_PriceListDetail_SelectPrice", parameters.ToArray());

            return Utility.DecimalParse(parameters.Last().Value);
        }

        /// <summary>
        /// Select price original
        /// </summary>
        /// <param name="supplierID">id of supplier to get price</param>
        /// <param name="carrierID">id of carrier to get zone and price</param>
        /// <param name="countryID">id of country to get zone</param>
        /// <param name="weight">weight to get price</param>
        /// <param name="date">date to get appropriate</param>
        /// <returns></returns>
        public decimal SelectOriginalPrice(int supplierID, int carrierID, int countryID, string type, decimal weight, DateTime date, out int priceListID)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("SupplierID", supplierID));
            parameters.Add(new SqlParameter("CarrierID", carrierID));
            parameters.Add(new SqlParameter("CountryID", countryID));
            parameters.Add(new SqlParameter("Type", type));
            parameters.Add(new SqlParameter("Weight", weight));
            parameters.Add(new SqlParameter("Date", date));
            parameters.Add(new SqlParameter("ListPrice", SqlDbType.Decimal, 100));
            parameters.Add(new SqlParameter("PriceListID", SqlDbType.Int, 100));

            parameters[6].Scale = 2;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;

            _helper.ExecuteNonQuery("proc_PriceListDetail_SelectOriginalPrice", parameters.ToArray());

            priceListID = Utility.IntParse(parameters[7].Value);
            return Utility.DecimalParse(parameters[6].Value);
        }

        /// <summary>
        /// Select price customer
        /// </summary>
        /// <param name="customerID">id of customer to get price</param>
        /// <param name="carrierID">id of carrier to get zone and price</param>
        /// <param name="countryID">id of country to get zone</param>
        /// <param name="weight">weight to get price</param>
        /// <param name="date">date to get appropriate</param>
        /// <returns></returns>
        public decimal SelectCustomerPrice(int customerID, int carrierID, int supplierID, int countryID, string type, decimal weight, DateTime date, out int priceListID)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("CustomerID", customerID));
            parameters.Add(new SqlParameter("CarrierID", carrierID));
            parameters.Add(new SqlParameter("SupplierID", supplierID));
            parameters.Add(new SqlParameter("CountryID", countryID));
            parameters.Add(new SqlParameter("Type", type));
            parameters.Add(new SqlParameter("Weight", weight));
            parameters.Add(new SqlParameter("Date", date));
            parameters.Add(new SqlParameter("ListPrice", SqlDbType.Decimal, 100));
            parameters.Add(new SqlParameter("PriceListID", SqlDbType.Int, 100));

            parameters[7].Scale = 2;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;

            _helper.ExecuteNonQuery("proc_PriceListDetail_SelectCustomerPrice", parameters.ToArray());

            priceListID = Utility.IntParse(parameters[8].Value);
            return Utility.DecimalParse(parameters[7].Value);
        }

        public void Delete(string priceListID)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("PriceListID", priceListID));

            _helper.ExecuteQuery("proc_PriceListDetail_Delete", parameters.ToArray());
        }
    }
}